package com.example.simpletask

import android.util.Log

class Mission {
    private var id: Int = 0
    private var content: String = ""
    private var steps: Int = 0
    private var stage: Int = 0
    private var attrs: Int = 0

//    此变量是在未接入数据库时使用的，接入数据库的逻辑后已废弃
    companion object {
        var s_lastid: Int = 0
    }

    constructor(id: Int, content: String, steps: Int, stage: Int, attrs: Int = 0) {
        this.id = id
        this.content = content

        if (steps > 0) {
            this.steps = steps
        } else {
            this.steps = 1
        }

        if (0 <= stage && stage <= this.steps)  // 若不满足条件则为默认的0
            this.stage = stage

        if (this.id > s_lastid) {
            s_lastid = this.id
        }

        if (attrs >= 0) {
            this.attrs = attrs
        }
    }

//    此构造函数在接入数据库后已废弃
    constructor(content: String, steps: Int, attrs: Int = 0) {
        this.id = ++s_lastid

        this.content = content

        if (steps > 0) {
            this.steps = steps
        } else {
            this.steps = 1
        }
        this.stage = 0

        if (attrs >= 0) {
            this.attrs = attrs
        }
    }

    public fun getId(): Int = this.id

    public fun getContent(): String = this.content
    public fun setContent(content: String) {
        this.content = content
    }

    public fun getSteps(): Int = this.steps
    public fun setSteps(steps: Int) {
        if (steps <= 0)
            return
        this.steps = steps
    }

    public fun getStage(): Int = this.stage
    public fun setStage(stage: Int) {
        if (stage > this.steps || stage < 0) {
            return
        }
        this.stage = stage
    }

    /**
     * 进度+1
     */
    public fun increaseStage() {
        if (stage + 1 <= steps) {
            stage = stage + 1
        }
    }

    /**
     * 进度-1
     */
    public fun decreaseStage() {
        if (stage - 1 >= 0) {
            stage = stage - 1
        }
    }

    /**
     * 通过位运算来得出来是否拥有这个属性
     */
    public fun hasAttr(attr: Int): Boolean {
        // 按位与，如果那一位上为1则操作完后数会大于0，否则为0
        return (this.attrs and attr) > 0
    }

    /**
     * 通过位运算添加属性
     */
    public fun addAttr(attrs: Int) {
        this.attrs = this.attrs or attrs
    }

    override fun toString(): String {
        return "Mission: id: " + id.toString() + ", content:" +
                content + ", steps:" + steps.toString() + ", stage:" + stage.toString()
    }
}